﻿using System;

namespace Katamino.Logic
{
    public class BitRotator
    {
        private int _width;
        private UInt64 _firstRowAllBits;
        private BitTransposer _transposer;

        public BitRotator(int width)
        {
            _width = width;
            _firstRowAllBits = (((UInt64)1) << width) - 1;

            _transposer = new BitTransposer(width);
        }

        public UInt64 Rotate(UInt64 pattern)
        {
            UInt64 result = 0;

            while (pattern > 0)
            {
                UInt64 firstRowBits = pattern & _firstRowAllBits;

                result = (result << 1) | _transposer.Transpose(firstRowBits);

                pattern >>= _width;
            }

            return result;
        }
    }
}